package com.jsgt.greekfire.ui.mainassets.coindetail

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.fhx.core.base.BaseFragment
import com.jsgt.greekfire.PAGE_SIZE
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.AssetDetailsResult
import com.jsgt.greekfire.bean.BillBean
import com.jsgt.greekfire.common.util.RecycleEmptyView
import com.jsgt.greekfire.factory.UserViewModelFactory
import com.jsgt.greekfire.ui.mainassets.coindetail.adapter.AssetDetailsAdapter
import com.jsgt.greekfire.viewmodel.UserViewModel
import com.scwang.smartrefresh.header.MaterialHeader
import com.scwang.smartrefresh.layout.footer.ClassicsFooter
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener
import com.scwang.smartrefresh.layout.listener.OnRefreshListener
import kotlinx.android.synthetic.main.fragment_coin_new_detail.*
import kotlinx.android.synthetic.main.view_title.*

private const val ARG_PARAM = "param"
private const val ARG_PARAM_ID = "param1"
private const val ARG_PARAM_SYMBOL = "param2"

class CoinNewDetailFragment : BaseFragment() {
    private val userViewModel: UserViewModel by lazy {
        ViewModelProvider(
            this,
            UserViewModelFactory()
        ).get(UserViewModel::class.java)
    }

    private var param1: Int? = null
    private var assetGuid: Int? = null
    private var symbol: String? = null
    private var page1 = 1
    private var page2 = 1
    private var page3 = 1
    private val limit = 20
    private val allDatas: ArrayList<BillBean> = ArrayList()
    private val incomeDatas: ArrayList<BillBean> = ArrayList()
    private val expenditureDatas: ArrayList<BillBean> = ArrayList()

    private val assetDetailsAdapter by lazy {
        AssetDetailsAdapter(context!!, R.layout.item_asset_details_list)
    }

    companion object {
        const val TYPE_ALL = 0 //全部
        const val TYPE_INCOME = 1 //收入
        const val TYPE_EXPENDITURE = 2 //支出

        @JvmStatic
        fun newInstance(param: Int, assetGuid: Int, symbol: String) =
            CoinNewDetailFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_PARAM, param)
                    putInt(ARG_PARAM_ID, assetGuid)
                    putString(ARG_PARAM_SYMBOL, symbol)
                }
            }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getInt(ARG_PARAM, -1)
            assetGuid = it.getInt(ARG_PARAM_ID, -1)
            symbol = it.getString(ARG_PARAM_SYMBOL)
        }
    }

    override fun getLayoutId(): Int = R.layout.fragment_coin_new_detail
    override fun main() {
        title_tv?.text = resources.getString(R.string.coin_new_detail_title)
        refresh.setRefreshHeader(
            MaterialHeader(context).setColorSchemeResources(
                R.color.main_theme_color
            )
        )
        refresh.setRefreshFooter(ClassicsFooter(context)) //设置Footer
        recycler.setLayoutManager(LinearLayoutManager(context))
        RecycleEmptyView.showEmptyView(
            activity as AppCompatActivity?,
            assetDetailsAdapter,
            "暂无明细",
            R.mipmap.icon_empty_no_data
        )
        recycler.adapter = assetDetailsAdapter
        assetDetailsAdapter.setSymbol(symbol!!)
        refresh.setOnRefreshListener(OnRefreshListener {
            if (param1 == TYPE_ALL) {
                page1 = 1
                recordList("0", page1)
            } else if (param1 == TYPE_INCOME) {
                page2 = 1
                recordList("1", page2)
            } else if (param1 == TYPE_EXPENDITURE) {
                page3 = 1
                recordList("2", page3)
            }

        })
        refresh.setOnLoadMoreListener(OnLoadMoreListener {
            if (param1 == TYPE_ALL) {
                page1++
                recordList("0", page1)
            } else if (param1 == TYPE_INCOME) {
                page2++
                recordList("1", page2)
            } else if (param1 == TYPE_EXPENDITURE) {
                page3++
                recordList("2", page3)
            }
        })

        refresh.autoRefresh()
        userViewModel.tradingRecordLiveData.observe(this, Observer {
            if (it != null) {
                if (param1 == TYPE_ALL) {
                    if (page1 == 1) {
                        allDatas!!.clear()
                    }
                    allDatas.addAll(it)
                    assetDetailsAdapter.setList(allDatas)
                    if (allDatas!!.size <= 0) {
                        RecycleEmptyView.showEmptyView(
                            activity as AppCompatActivity?,
                            assetDetailsAdapter,
                            "暂无明细",
                            R.mipmap.icon_empty_no_data
                        )
                    }
                } else if (param1 == TYPE_INCOME) {
                    if (page2 == 1) {
                        incomeDatas!!.clear()
                    }
                    incomeDatas.addAll(it)
                    assetDetailsAdapter.setList(incomeDatas)
                    if (incomeDatas!!.size <= 0) {
                        RecycleEmptyView.showEmptyView(
                            activity as AppCompatActivity?,
                            assetDetailsAdapter,
                            "暂无明细",
                            R.mipmap.icon_empty_no_data
                        )
                    }
                } else if (param1 == TYPE_EXPENDITURE) {
                    if (page2 == 1) {
                        expenditureDatas!!.clear()
                    }
                    expenditureDatas.addAll(it)
                    assetDetailsAdapter.setList(expenditureDatas)
                    if (expenditureDatas!!.size <= 0) {
                        RecycleEmptyView.showEmptyView(
                            activity as AppCompatActivity?,
                            assetDetailsAdapter,
                            "暂无明细",
                            R.mipmap.icon_empty_no_data
                        )
                    }
                }
                if (it.size < limit) {
                    refresh.finishLoadMoreWithNoMoreData();
                } else {
                    refresh.finishLoadMore();
                    refresh.finishRefresh(true);
                }
            } else {
                refresh.finishLoadMoreWithNoMoreData();
            }
        })
        userViewModel.errorLiveData.observe(this, Observer {
            showProgress(false)
            showToast(it.toString())
            if (param1 == TYPE_ALL) {
                if (allDatas!!.size <= 0) {
                    if (page1 > 1) {
                        page1--;
                        refresh.finishLoadMore(false);
                    }
                }
            } else if (param1 == TYPE_INCOME) {
                if (incomeDatas!!.size <= 0) {
                    if (page2 > 1) {
                        page2--;
                        refresh.finishLoadMore(false);
                    }
                }
            } else if (param1 == TYPE_EXPENDITURE) {
                if (expenditureDatas!!.size <= 0) {
                    if (page3 > 1) {
                        page3--;
                        refresh.finishLoadMore(false);
                    }
                }
            }
            refresh.finishRefresh();

        })
    }


    private fun recordList(events: String, page: Int) {
        userViewModel.tradingRecord(assetGuid.toString(), events, page, PAGE_SIZE)
    }

}
